package com.example.qixinshangwutong.model.news;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.example.qixinshangwutong.R;
import com.example.qixinshangwutong.view.activity.NewsActivity;

import java.util.List;

public class FmentAdapter extends RecyclerView.Adapter <FmentAdapter.FmentHolder> {
    List<UILunBo> data;
    Context context;
    String name;

    public void setData(List<UILunBo> list) {
        this.data = list;
    }

    public FmentAdapter(Context context) {
        this.context = context;
    }

    @Override
    public FmentHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        return new FmentHolder(LayoutInflater.from(context).inflate(R.layout.first_list_help, null));
    }

    @Override
    public void onBindViewHolder(@NonNull FmentAdapter.FmentHolder holder, int position) {
        Glide.with(context).load(data.get(position).getUrl()).into(holder.img);
        name = data.get(position).getTitle();
        holder.tvhead.setText(name);
        holder.tvtime.setText(data.get(position).getAdd_time());
        holder.itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Intent intent = new Intent(context, NewsActivity.class);
                intent.putExtra("name", name);
                intent.putExtra("uri", "http://android.qxswt.com/mediadetail/" + data.get(position).getId());
                context.startActivity(intent);
            }
        });

    }

    @Override
    public int getItemCount() {
        return null != data ? data.size() : 0;
    }

    class FmentHolder extends RecyclerView.ViewHolder {
        ImageView img;
        TextView tvhead, tvtime;

        public FmentHolder(View itemView) {
            super(itemView);
            img = itemView.findViewById(R.id.thrid_list_help_iv);
            tvhead = itemView.findViewById(R.id.thrid_list_help_headtv);
            tvtime = itemView.findViewById(R.id.thrid_list_help_timetv);
        }
    }
}
